package dialogs
{
	import Classes.pi_images;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_button;
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class pi_checkFriends extends pi_friends
	{
	    private var _selected: Array;
	    public static const SELECTUSERS: String = 'SELECTUSERS';
	    
		public function pi_checkFriends(parent:uif_component, ax:int=0, ay:int=0) {
			super(parent, ax, ay);
			_selected = new Array();
			var inner: Rectangle = getInner();
			var ok: uif_button = new uif_button(this, inner.x + inner.width - 80, 
										inner.x + inner.height - 30, 80, 30, 'button');
			ok.text = 'ok';
			ok.addEventListener(MouseEvent.CLICK, doSelectUsers);										
		}
		
		protected function doSelectUsers(e: Event=null): void {
			if (_selected.length > 0) dispatchEvent(new ObjectEvent(SELECTUSERS, _selected));
			doClose(e);
		}
		
		public function get Selected(): Array {
			return _selected;
		}
		
		protected function isSelect(user: Object): Boolean {
			return _selected.indexOf(user) > -1;
		}
		
/*
		override protected function userItemEvents(item: pi_user): pi_user {
			item.addEvent(MouseEvent.DOUBLE_CLICK, doSelectUsers);
			return super.userItemEvents(item);
		}
*/		
		
		override protected function doSelectUser(e: MouseEvent): void {
			var userButton: pi_user = e.target as pi_user;
			if (isSelect(userButton.userData)) {
				userButton.removeChildAt(userButton.numChildren - 1);
				_selected.splice(_selected.indexOf(userButton.userData), 1);
			} else {
				var inner: Rectangle = userButton.getInner();
				var check: * = new pi_images.c_check();
				check.x = inner.x;
				check.y = inner.y + inner.height - check.height;
				userButton.addChild(check);
				_selected.push(userButton.userData);
			}
			super.doSelectUser(e);
		}
		
	}
}